import { PrismaClient } from '@prisma/client'
import bcrypt from 'bcryptjs'
import dotenv from 'dotenv'

// 加载环境变量
dotenv.config()

const prisma = new PrismaClient()

async function main() {
    try {
        // 创建管理员用户
        const adminEmail = process.env.ADMIN_EMAIL || 'admin@example.com'
        const adminPassword = process.env.ADMIN_PASSWORD || 'admin123'
        const hashedAdminPassword = await bcrypt.hash(adminPassword, 10)

        const admin = await prisma.user.upsert({
            where: { email: adminEmail },
            update: {
                password: hashedAdminPassword
            },
            create: {
                email: adminEmail,
                username: 'Admin',
                password: hashedAdminPassword,
                role: 'admin'
            }
        })

        console.log('Admin user created:', admin)

        // 创建测试用户
        const testEmail = process.env.TEST_EMAIL || 'test@example.com'
        const testPassword = process.env.TEST_PASSWORD || 'test123'
        const hashedTestPassword = await bcrypt.hash(testPassword, 10)

        const testUser = await prisma.user.upsert({
            where: { email: testEmail },
            update: {
                password: hashedTestPassword
            },
            create: {
                email: testEmail,
                username: 'Test User',
                password: hashedTestPassword,
                role: 'user'
            }
        })

        console.log('Test user created:', testUser)

    } catch (error) {
        console.error('Seed error:', error)
        throw error
    }
}

main()
    .catch((e) => {
        console.error(e)
        process.exit(1)
    })
    .finally(async () => {
        await prisma.$disconnect()
    }) 